<template>
  <div>
    <div class="index">
      <div class="container">
        <div class="swiper-box">
          <!-- 轮播左侧导航菜单 -->
          <div class="nav-menu">
            <ul class="menu-warp">
              <li class="menu-item">
                <a href="javascript:;">电视 盒子</a>
                <div class="children">
                  <ul v-for="item in menuList" :key="item.id">
                    <li v-for="sub in item" :key="sub.id">
                      <a :href="sub ? 'product/' + sub.id : ' '">
                        <img
                          :src="sub ? sub.image : '/imgs/item-box-1.png'"
                          alt=""
                        />
                        {{ sub ? sub.name : "小米9" }}
                      </a>
                    </li>
                  </ul>
                </div>
              </li>
              <li class="menu-item">
                <a href="javascript:;">笔记本 平板</a>
              </li>
              <li class="menu-item">
                <a href="javascript:;">家电 插线板</a>
              </li>
              <li class="menu-item">
                <a href="javascript:;">出行 穿戴</a>
              </li>
              <li class="menu-item">
                <a href="javascript:;">智能 路由器</a>
              </li>
              <li class="menu-item">
                <a href="javascript:;">电源 配件</a>
              </li>
              <li class="menu-item">
                <a href="javascript:;">生活 箱包</a>
              </li>
            </ul>
          </div>
          <!-- 轮播区域 -->
          <swiper :options="swiperOption">
            <swiper-slide v-for="item in slideList" :key="item.id">
              <a :href="'/product/' + item.id"><img :src="item.img"/></a>
            </swiper-slide>
            <div class="swiper-pagination" slot="pagination"></div>
            <div class="swiper-button-prev" slot="button-prev"></div>
            <!--左箭头。如果放置在swiper-container外面，需要自定义样式。-->
            <div class="swiper-button-next" slot="button-next"></div>
            <!--右箭头。如果放置在swiper-container外面，需要自定义样式。-->
          </swiper>
        </div>
        <div class="ads-box">
          <a
            :href="'/product/' + item.id"
            v-for="(item, index) in adsList"
            :key="index"
          >
            <img :src="item.image" alt />
          </a>
        </div>
        <div class="banner">
          <a :href="'/product/' + banner.id">
            <img :src="banner.image" alt />
          </a>
        </div>
        <div class="product-box">
          <div class="container">
            <h2>手机</h2>
            <div class="wrapper">
              <div class="banner-left">
                <a href="/product/35">
                  <img src="/imgs/mix-alpha.jpg" alt="" />
                </a>
              </div>
              <div class="list-box">
                <div class="list" v-for="(item, i) in phoneList" :key="i">
                  <div class="item" v-for="(urr, j) in item" :key="j">
                    <span :class="{ 'new-pro': j % 2 == 0 }">新品</span>
                    <div class="item-img">
                      <img :src="urr.mainImage" alt="" />
                    </div>
                    <div class="item-info">
                      <p class="info">{{ urr.name }}</p>
                      <p>{{ urr.subtitle }}</p>
                      <p class="price" @click="addCart(urr.id)">
                        {{ urr.price }}
                      </p>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <server-bar></server-bar>
    <modal
      title="提示"
      sureText="查看购物车"
      btnType="1"
      modalType="middle"
      :showModal="showModal"
      @submit="gotoCart()"
      @cancel="showModal = false"
    >
      <!-- 使用插槽 -->
      <template slot="body">
        <p>商品添加成功！</p>
      </template>
    </modal>
  </div>
</template>

<script>
import ServerBar from "../components/serverBar.vue";
import Modal from "../components/Modal.vue";
import { Swiper, SwiperSlide } from "vue-awesome-swiper";
// import "swiper/css/swiper.css"; //使用swiper的样式方法。
export default {
  name: "index",
  components: {
    ServerBar,
    Swiper,
    SwiperSlide,
    Modal,
  },
  filters: {
    currency(val) {
      if (!val) return "0.00";
      return "￥" + val.toFixed(2) + "元";
    },
  },
  data() {
    return {
      swiperOption: {
        autoplay: true, //环绕
        loop: true, //自动切换
        effect: "cube", //播放的形式方块。
        cubeEffect: {
          slideShadows: true,
          shadow: true,
          shadowOffset: 100,
          shadowScale: 0.6,
        },
        //分页器
        pagination: {
          el: ".swiper-pagination",
          clickable: true, //点击原点可切换
        },
        //箭头实现切换效果
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
      },
      slideList: [
        {
          id: "42",
          img: "/imgs/slider/slide-1.jpg",
        },
        {
          id: "45",
          img: "/imgs/slider/slide-2.jpg",
        },
        {
          id: "46",
          img: "/imgs/slider/slide-3.jpg",
        },
        {
          id: "",
          img: "/imgs/slider/slide-4.jpg",
        },
        {
          id: "",
          img: "/imgs/slider/slide-4.jpg",
        },
      ],
      menuList: [
        [
          {
            id: 30,
            image: "/imgs/item-box-1.png",
            name: "小米CC9",
          },
          {
            id: 31,
            image: "/imgs/item-box-2.png",
            name: "小米8青春版",
          },
          {
            id: 32,
            image: "/imgs/item-box-3.jpg",
            name: "Redmi K20 Pro",
          },
          {
            id: 33,
            image: "/imgs/item-box-4.jpg",
            name: "移动4G专区",
          },
        ],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
      ],
      adsList: [
        {
          id: "33",
          image: "/imgs/ads/ads-1.png",
        },
        {
          id: "48",
          image: "/imgs/ads/ads-2.jpg",
        },
        {
          id: "45",
          image: "/imgs/ads/ads-3.png",
        },
        {
          id: "47",
          image: "/imgs/ads/ads-4.jpg",
        },
      ],
      banner: {
        id: "30",
        image: "/imgs/banner-1.png",
      },
      phoneList: [],
      showModal: false,
    };
  },
  mounted() {
    console.log("this.$store.state.username");
    console.log(this.$store.state.username);
    this.init();
  },
  methods: {
    init() {
      this.axios
        .get("/products", { parmas: { categoryId: 100012, pageSize: 16 } })
        .then((res) => {
          res.list = res.list.slice(2, 16);
          this.phoneList = [res.list.slice(0, 4), res.list.slice(4, 8)];
        });
    },
    addCart(id) {
      this.showModal = true;
      this.axios
        .post("/carts", {
          productId: id,
          selected: true,
        })
        .then((res) => {
          this.showModal = true;
        })
        .catch((err) => {
          this.showModal = true;
          this.$store.dispatch("saveCartCount", res.cartTotalQuantity); //实时更新购物车数量
        });
    },
    gotoCart() {
      this.$router.push("/cart");
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../assets/scss/mixin";
@import "../assets/scss/config";
.index {
  .swiper-box {
    .nav-menu {
      position: absolute;
      width: 264px;
      height: 451px;
      left: 0px;
      z-index: 11;
      padding: 26px 0px;
      background-color: rgba(85, 88, 90, 0.48);
      box-sizing: border-box;
      .menu-warp {
        .menu-item {
          height: 50px;
          line-height: 50px;

          a {
            position: relative;
            color: #ffffff;
            font-size: 16px;
            padding-left: 30px;
            display: block;
            &:after {
              @include bgImg(16px, 16px, "../../public/imgs/icon-arrow.png");
              position: absolute;
              top: 17.5px;
              right: 30px;
              content: " ";
            }
          }
          &:hover {
            background-color: $colorA;
            .children {
              display: block;
            }
          }
          .children {
            display: none;
            width: 962px;
            height: 451px;
            background-color: $colorG;
            position: absolute;
            top: 0px;
            left: 264px;
            background-color: $colorG;
            border: 1px solid $colorH;
          }
          ul {
            display: flex;
            justify-content: space-between;
            height: 75px;
            li {
              height: 75px;
              line-height: 75px;
              flex: 1;
              padding-left: 23px;
            }
            img {
              width: 42px;
              height: 35px;
              vertical-align: middle;
              margin-right: 15px;
            }
            a {
              display: inline-block;
              color: #333333;
              font-size: 14px;
            }
          }
        }
      }
    }
    .swiper-container {
      height: 451px;
      .swiper-button-prev {
        left: 274px;
      }
      .img {
        height: 100%;
        width: 100%;
      }
    }
  }
  .ads-box {
    @include flex();
    margin-top: 14px;
    margin-bottom: 31px;
    img {
      width: 294px;
      height: 166px;
      display: inline-block;
    }
  }
  .banner {
    height: 170px;
    a {
      display: inline-block;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  .product-box {
    width: 100%;
    background-color: #f5f5f5;
    padding-bottom: 10px;
    h2 {
      font-size: 22px;
      padding-bottom: 20px;
      padding-top: 20px;
      display: inline-block;
      color: #333333;
    }
    .wrapper {
      height: 619px;
      display: flex;
      .banner-left {
        margin-right: 14px;
        a {
          width: 224px;
          height: 619px;
          img {
            display: inline-block;
            width: 100%;
            height: 100%;
          }
        }
      }
      .list-box {
        .list {
          @include flex();
          width: 986px;
          margin-bottom: 16px;
          &:last-child {
            margin-bottom: 0px;
          }
          .item {
            width: 236px;
            height: 302px;
            background-color: $colorG;
            text-align: center;
            span {
              display: inline-block;
              width: 67px;
              height: 24px;
              font-size: 14px;
              color: #ffffff;
              line-height: 24px;
              &.new-pro {
                background-color: #7ecf68;
              }
              &.kill-pro {
                background-color: #e82626;
              }
            }
            .item-img {
              img {
                height: 170px;
                width: 100%;
              }
            }
            .item-info {
              color: #999999;
              margin-bottom: 10px;
              margin-top: 6px;
              .info {
                color: $colorB;
              }
              p {
                display: inline-block;
                color: $colorC;
                font-size: 14px;
                display: block;
                font-weight: bold;
              }
              .price {
                color: #f20a0a;
                font-size: $fontJ;
                display: block;
                font-weight: bold;
                cursor: pointer;
                &:after {
                  @include bgImg(20px, 30px, "/imgs/icon-cart-hover.png");
                  margin-left: 5px;
                  vertical-align: middle;
                  content: "";
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>
